var data = [] //所有产品数据

var currentIndex = 0  //当前产品索引

var currentSkuIndex = 0

var targetUrl = ''

var tabId = -1

var productEntity = null

var imgsCache = {}

var failList = [] // 上传失败参数记录

var noStandardsList = [] // 不符合标准 并未进行上传的商品记录

var userInfo = new Object() // 用户信息 从cookie获取

var uploadRequestUrl = '' // 接口域名

var env = 'dev' // 环境

var accountPlatform = '' // 账号所属的平台

var unit = new Set(`码, 包, 盆, 片, 把, 克, 双, 其它, 千米, 盒, 套, 盘, 枚, 厘米, 枝, 张, 条, 令, 立方米, 座
, 千克, 个, 副, 台, 箱, 米, 棵, 件, 卷, 根, 毫升, 平方厘米, 捆, 袋, 位, 排, 英寸, 吨, 只, 支, 瓶, 升, 千个, 平方米, 株, 桶, 顶`.split(',').map(v=>v.trim()))

// 配置
var config = {
  "newsDetail": "http://gyp.youboy.com/h5/news/appdetail?id=",
  "userAgreementUrl": "http://gyp.youboy.com/h5/service/userAgreement",
  "productDetail": "https://gyp.youboy.com/h5/product/appdetail?id=",
  "imageDomain": "https://yhby-oss.oss-cn-shenzhen.aliyuncs.com",
  "productShare": "https://gyp.youboy.com/h5/product/detail?id=",
  "factoryShare": "https://gyp.youboy.com/h5/shop/index?id=",
  "serviceCenterUrl": "http://gyp.youboy.com/h5/service/serviceCenter",
  "newsShare": "https://gyp.youboy.com/h5/news/detail?id="
 }

 var factoryInfo = {}

 var setting = {} // 所有产品通用参数

chrome.webNavigation.onCompleted.addListener(function(tab) {
  if (tab.tabId === tabId) {
    chrome.tabs.sendMessage(tab.tabId, { action: 'completed', url: tab.url })
  }
})

// 监听并接收content-script发送的消息
chrome.extension.onMessage.addListener(
  function(req, sender, res) {
    switch (req.cmd) {
      case 'getTemplate':
        getTemplate(chrome.extension.getURL(req.url), res)
        break
      case 'getUserInfo':
        res(userInfo)
        break
      case 'getFactoryInfo':
        getFactoryInfo(res)
        break
      case 'getFormData':
        getFormData(req, res)
        break
      case 'getGYPConfig':
        getGYPConfig(req, res)
        break
      case 'getAccountPlatform':
        res(accountPlatform)
        break
    }

    chrome.tabs.getSelected(null, function(tab) {
      switch (req.cmd) {
        case 'next':
          getNextPageInfo(req)
          break
        case 'nextSamePage':
          pushData(req)
          break
        case 'end':
          getPageOver(req)
          break
        case 'upload':
          uploadDataToGYP(req)
          break
        case 'clear':
          reset()
          break
        case 'gather':
          gather(req)
          break
        case 'mikecrm-to-detail':
          openMikecrmDetailPage(req)
          break
        case 'set-keyword-data':
          setMikecrmKeywordData(req)
          break
        case 'get-keyword-next':
          openMikecrmDetailNextPage(req)
          break
      }
    })
  }
)

// 关闭正在爬取数据的标签页时 已获取的列表和商品信息会清空
chrome.tabs.onRemoved.addListener(
  function(tab) {
    if (tab === tabId) {
      reset()
    }
  }
)

chrome.tabs.onSelectionChanged.addListener(getAccountPlatform)

function getAccountPlatform(tab) {
  chrome.tabs.sendMessage(tab, { action: 'getAccountPlatform', platform: accountPlatform })
}
